#define _CRT_SECURE_NO_WARNINGS 1

#include "../include/SeqList.h"

#define REALLOC(oldAddr, type, nums) \
    (type *)realloc(oldAddr, nums * sizeof(type))

void SLInit(SL *ps)
{
    // 初始化
    ps->arr = NULL;
    ps->size = ps->capacity = 0;
}
void SLDestroy(SL *ps)
{
    // 回收资源
    free(ps->arr);
    ps->arr = NULL;
    ps->size = ps->capacity = 0;
}
int IsEmpty(SL *ps)
{
    // 判空
    return ps->capacity == 0;
}

static void Check_SL_IsFull(SL *ps)
{
    // 检查空间是否充足，不足就扩容
    if (ps->size == ps->capacity)
    {
        int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
        elemType *newAddr = REALLOC(ps->arr, elemType, newcapacity);
        if (!newAddr)
        {
            printf("%s::%d::fail to realloc\n", __FILE__, __LINE__);
            return;
        }
        ps->arr = newAddr;
        ps->capacity = newcapacity;
    }
}

void SLPushBack(SL *ps, elemType x)
{
    // 尾插
    // Check_SL_IsFull(ps);
    // ps->arr[ps->size++] = x;

    // 复用 SLInsert
    SLInsert(ps, ps->size, x);
}
void SLPopBack(SL *ps)
{
    // 尾删
    // if (ps->size != 0)
    //     ps->size--;

    // 复用 SLErase
    SLErase(ps, ps->size - 1);
}
void SLPushFront(SL *ps, elemType x)
{
    // 头插
    // Check_SL_IsFull(ps);
    // // 挪动数据
    // int end = ps->size; // end 指向最后一个元素的下一个位置
    // while (end != 0)
    // {
    //     ps->arr[end] = ps->arr[end - 1];
    //     --end;
    // }
    // ps->arr[0] = x;
    // ps->size++;

    // 复用 SLInsert
    SLInsert(ps, 0, x);
}
void SLPopFront(SL *ps)
{
    // 头删
    // if (ps->size != 0)
    // {
    //     // 挪动数据
    //     int begin = 0, end = ps->size - 1;
    //     while (begin != end)
    //     {
    //         ps->arr[begin] = ps->arr[begin + 1];
    //         ++begin;
    //     }
    //     ps->size--;
    // }

    // 复用 SLErase
    SLErase(ps, 0);
}
void SLInsert(SL *ps, int pos, elemType x)
{
    // 插入
    // 检查 pos
    if (pos < 0 || pos > ps->size)
    {
        printf("%s::%d::the argu pos %d is invalid.\n",
               __FILE__, __LINE__, pos);
        return;
    }
    Check_SL_IsFull(ps);
    // 挪动数据
    int end = ps->size; // end 指向最后一个元素的下一个位置
    while (end != pos)
    {
        ps->arr[end] = ps->arr[end - 1];
        --end;
    }
    ps->arr[pos] = x;
    ps->size++;
}
void SLErase(SL *ps, int pos)
{
    // 删除
    // 检查 pos
    if (pos < 0 || pos > ps->size)
    {
        printf("%s::%d::the argu pos %d is invalid.\n",
               __FILE__, __LINE__, pos);
        return;
    }
    // 挪动数据
    int begin = pos, end = ps->size - 1;
    while (begin != end)
    {
        ps->arr[begin] = ps->arr[begin + 1];
        ++begin;
    }
    ps->size--;
}
int SLFindByValue(SL *ps, elemType x)
{
    // 查索引
    for (int i = 0; i < ps->size; ++i)
        if (ps->arr[i] == x)
            return i;
    return -1;
}
void Reverse(SL *ps)
{
    // 逆序
    int l = 0, r = ps->size - 1;
    elemType tmp;
    while (l < r)
    {
        tmp = ps->arr[l];
        ps->arr[l] = ps->arr[r];
        ps->arr[r] = tmp;
        ++l;
        --r;
    }
}
void SLPrint(SL *ps)
{
    // 输出
    int n = ps->size;
    for (int i = 0; i < 10 && i < n; ++i)
        printf("%d ", ps->arr[i]);
}